package Tools;

import jade.core.AID;

import java.io.Serializable;
import java.util.ArrayList;

import Messages.Definitions;

public class PolarRepresentation implements InternalRepresentation, Serializable {

	public String obj;
	ArrayList<InternalStatement> ir;
	Graph gr;
	public AID aid;
	
	//opinion=NOT a1 AND a2 obj=o1 operator=Know
	public PolarRepresentation(String obj) {
		this.obj = obj;
		ir = new ArrayList<InternalStatement>();
		gr = new Graph();
		aid=new AID(); //Anonymous
	}
	
	private int[] getArr(String[] strs){
		int[] arr = new int[4];
		for(String str:strs){
			String[] tokens = str.split(" ");
			
			int val = 0;
			if(tokens[0].toLowerCase().equals("pos"))
				val = 1;
			if(tokens[0].toLowerCase().equals("bel"))
				val = 2;
			if(tokens[0].toLowerCase().equals("know"))
				val = 3;
			
			int pos = 0; // x1 and x2 => 0
			
			if(tokens.length==1){
				System.out.println("SOME EMPTY STRING!!! "+str);
			}
			
			if(tokens[1].equals("NOT")){ 
				pos += 1;
				if(tokens[4].equals("NOT")) //not x1 and not x2 => 3
					pos+=2;
				// not x1 and x2 => 1
			}
			else
				if(tokens[3].equals("NOT")) //x1 and not x2 => 2
					pos+=2;
			
			
			arr[pos] = val;
			
			
		}
		return arr;
	} 
	
	public int convertToInternalRepresentation(String[] opinions) {
		ir = new ArrayList<InternalStatement>();
		
		//for(String s : opinions){
		ArrayList<int[]>tmp=gr.getPossibleOpinions(getArr(opinions));
		for(int[] x:tmp)
			ir.add(new InternalStatement(x, (double)1/tmp.size()));
		//}
		
		return 0;
	}
	
	public static double[][] toInterval(InternalStatement i){
		double[][] temp=new double[4][2];
		temp[0]=Definitions.intervals[i.values[0]];
		temp[1]=Definitions.intervals[i.values[1]];
		temp[2]=Definitions.intervals[i.values[2]];
		temp[3]=Definitions.intervals[i.values[3]];
		return temp;
	}
	
	public ArrayList<double[][]> getIntervalFormat(){
		ArrayList<double[][]> ret = new ArrayList<double[][]>();
		for(InternalStatement i:ir){
			double[][] temp=new double[4][2];
			temp[0]=Definitions.intervals[i.values[0]];
			temp[1]=Definitions.intervals[i.values[1]];
			temp[2]=Definitions.intervals[i.values[2]];
			temp[3]=Definitions.intervals[i.values[3]];
			ret.add(temp);
		}
		return ret;
	}
	
	public static String toSentence(int[] intRep){
		String ret="";
		
		for(int i=0;i<4;i++){
			switch(intRep[i]){
				case 0:{
					break;
				}
				case 1:{
					ret+="Pos";
					break;
				}
				case 2:{
					ret+="Bel";
					break;
				}
				case 3:{
					ret+="Know";
					break;
				}
			}
			if(intRep[i]!=0){
				switch(i){
					case 0:{
						ret+="(a1 AND a2) ";
						break;
					}
					case 1:{
						ret+="(NOT a1 AND a2) ";
						break;
					}
					case 2:{
						ret+="(a1 AND NOT a2) ";
						break;
					}
					case 3:{
						ret+="(NOT a1 AND NOT a2) ";
						break;
					}
				}
			}
		}
		return ret;
	}
	
	
	public String toString(){
		String s="\n";
		for(InternalStatement i:ir){
			for(int j=0;j<i.values.length;j++){
				s+=i.values[j]+" ";
			}
			s+='\n';
		}
		return s;
	}
}
